package com.link.tools.wxpay.mini;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(WXMiniConfig.class)
public class WxMiniConfiguration {
    private final WXMiniConfig wxConfig;

    @Autowired
    public WxMiniConfiguration(@Qualifier("wx.miniapp.config-com.link.tools.wxpay.mini.WXMiniConfig") WXMiniConfig wxConfig) {
        this.wxConfig = wxConfig;
    }

    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxConfig.getAppId());
        payConfig.setMchId(wxConfig.getMchId());
        payConfig.setMchKey(wxConfig.getMchKey());
        payConfig.setApiV3Key(wxConfig.getMchKey());
        payConfig.setKeyPath(wxConfig.getKeyPath());
        payConfig.setPrivateCertPath(wxConfig.getKeyPath() + "apiclient_cert.pem");
        payConfig.setPrivateKeyPath(wxConfig.getKeyPath() + "apiclient_key.pem");
        payConfig.setKeyPath(wxConfig.getKeyPath() + "apiclient_cert.p12");
        // payConfig.setSignType("HMAC-SHA256");
//        payConfig.setSignType("MD5");
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}